﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace ttg_internal.Utilities.UsageVisualizer.Other.Controls
{
    /// <summary>
    /// Interaction logic for BarGraph.xaml
    /// </summary>
    public partial class BarGraph
    {
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        public BarGraph()
        {
            InitializeComponent();
        }

        // Dependency properties
        public static readonly DependencyProperty PercentageProperty = DependencyProperty.Register("Percentage", typeof(double), typeof(BarGraph), new PropertyMetadata(0d, PercentageChanged));

        // Private fields
        private static readonly TimeSpan AnimationDuration = TimeSpan.FromSeconds(.5);

        /// <summary>
        /// Percentage to show
        /// </summary>
        public double Percentage
        {
            get { return (double)GetValue(PercentageProperty); }
            set
            {
                SetValue(PercentageProperty, value);
            }
        }

        /// <summary>
        /// Callback that is called when Percentage has changed
        /// </summary>
        private static void PercentageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Debug.Assert(d is BarGraph);
            var barGraph = ((BarGraph)d);

            barGraph.BarTransform.BeginAnimation(ScaleTransform3D.ScaleYProperty, new DoubleAnimation(barGraph.Percentage, AnimationDuration));
        }
    }
}
